/* Copyright (C) Abu Rizal, 2009.
 * 
 * This file is part of QurText.
 * 
 * QurText is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * QurText is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with QurText. If not, see <http://www.gnu.org/licenses/>.
 */
package qurtext.client;

import java.io.Serializable;

@SuppressWarnings("serial")
public class ClientUser implements Serializable{
	public String email;
	public String loginUrl;
	public boolean isAdmin;
	public boolean[] sectionRead=new boolean[558];
	public String lastSection;
	public boolean isLoggedIn() {
		return null!=email;
	};
	public ClientUser(String email, String loginUrl, boolean isAdmin, String sectionReadCookie, String lastSection) {
		this.email = email;
		this.loginUrl = loginUrl;
		this.isAdmin = isAdmin;
		this.setSectionReadCookie(sectionReadCookie);
		this.lastSection = lastSection;
	}
	public ClientUser() {
	}
	public int getSectionCount() {
		int result=0;
		for (int i=0;i<sectionRead.length;i++) {
			if (sectionRead[i]) result++;
		}
		return result;
	}

	public String getSectionReadCookie() {
		String result="";
		boolean currentBit=false;
		int currentCount=0;
		for (int i=0;i<sectionRead.length;i++) {
			if (sectionRead[i]==currentBit) {
				currentCount++;
			} else {
				currentBit=sectionRead[i];
				result+=currentCount+":";
				currentCount=1;
			}
		}
		result+=currentCount;
		return result;
	}
	
	public void setSectionReadCookie(String text) {
		if (null==text) return;
		String[] parts=text.split("[:]");
		boolean currentBit=false;
		int i=0;
		for (String part:parts) {
			int repeatCount=Integer.valueOf(part);
			for (int j=0;j<repeatCount;j++) {
				sectionRead[i]=currentBit;
				i++;
			}
			currentBit=!currentBit;//flip
		}
	}
}
